<?php
class Auctions extends CActiveRecord
{
	public $inventory_last_read;
	public $inventory_last_time;
	public $product_name;
	public $site_name;
	public $capacity;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'auctions';
	}
	
	public function rules()
	{
		return array(
			array('user_ID, tank_ID, site_ID, product_ID, status_ID, gallon, auction_start, auction_end, delivery_due', 'required', 'on'=>'create'),
			array('user_ID, tank_ID, site_ID, product_ID, status_ID', 'numerical', 'integerOnly'=>true, 'on'=>'create'),
			array('gallon', 'numerical', 'numberPattern'=>'/^\d{1,9}+(\.\d{1,6})?$/', 'message'=>'the gallon field at most has 9 digits before the decimal point and 6 digits after the decimal point', 'on'=>'create'),
			array('tank_ID', 'numerical', 'min'=>0, 'on'=>'create', 'tooSmall'=>'please select a valid tank'),
			array('gallon', 'numerical', 'min'=>0, 'on'=>'create'),
			array('auction_start, auction_end, delivery_due', 'date', 'format'=>'yyyy-MM-ss hh:mm:ss', 'on'=>'create'),
			array('notes', 'length', 'max'=>200, 'on'=>'create'),
			array('purchase_order_number', 'length', 'max'=>20, 'on'=>'create'),
		);
	}
	
	public function relations()
	{
		return array(
			'user'=>array(self::BELONGS_TO, 'Users', 'user_ID'),
			'tank'=>array(self::BELONGS_TO, 'Tanks', 'tank_ID'),
			'site'=>array(self::BELONGS_TO, 'Sites', 'site_ID'),
			'product'=>array(self::BELONGS_TO, 'Products', 'product_ID'),
			'status'=>array(self::BELONGS_TO, 'Status', 'status_ID'),
			'bidAuctions'=>array(self::HAS_MANY, 'BidAuctions', 'auction_ID'),
			'order'=>array(self::HAS_ONE, 'Orders', 'auction_ID'),
		);
	}
	
	public function attributeLabels()
	{
		return array(
			'gallon'=>'The amount of gallon',
			'auction_start'=>'Auction start time',
			'auction_end'=>'Auction end time',
			'delivery_due'=>'delivery due time',
		);
	}
	
	public function getAuction($auctionID) {
		$sql = "SELECT * FROM ".$this->tableName()." WHERE auction_ID='".$auctionID."'";
		$resultset = $this->findAllBySql($sql);
		
		return $resultset;
	}
	
	public function getAuctionOwner($auctionID) {
		$sql = "SELECT user_ID FROM ".$this->tableName()." WHERE auction_ID='".$auctionID."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach ($resultset as $record) {
			$userID = $record->user_ID;
		}
		
		$resultset = Users::model()->getName($userID);
		
		foreach ($resultset as $row) {
			$owner = $row->contact_first_name." ".$row->contact_last_name;
		}
		
		return $owner;
	}
}
?>
